Use case: User application program for OBK area in polling mode

Use case applicable in HAL2 for both FLASH and NVM.

Sequence diagram

User application program OBK area in polling mode (HAL1)

@startuml
title: User application program OBK area in polling mode (HAL1)

hide footbox
participant "User Application" as User
participant "FLASH" as Flash
participant "SBS" as SBS

group #snow FLASH Unlock
   User -> Flash : HAL_FLASH_Unlock()
end

group #Moccasin SBS HDPL Configuration and Unlock OBK area
   User -> SBS : HAL_SBS_SetOBKHDPL()
   note right: x : HDP Levels
   User -> Flash : HAL_FLASHEx_OBK_Unlock()
end

group #snow FLASH Erase Operation
   User -> Flash : HAL_FLASHEx_Erase(OBK alternate area)
   note right: Erase OBK alternate area sectors
end

group #snow FLASH Swap Operation
   User -> Flash : HAL_FLASHEx_OBK_Swap()
   note right: Swap OBK sectors between current and alternate
end

group #snow FLASH Program Operation (loop)
   loop while Address < HDPLx_END_ADDRESS
      User -> Flash : HAL_FLASH_Program(FLASH_TYPEPROGRAM_GRANULARITY_ALT, Address, Granularity)
      alt Program success (HAL_OK)
         User --> User : Address = Address + offset
      else Program failure
         User --> User : Handle error or retry
      end
   end
end

group #Moccasin FLASH Lock OBK area
   User -> Flash : HAL_FLASHEx_OBK_Lock()
end

group #snow Lock FLASH (recommended)
   User -> Flash : HAL_FLASH_Lock()
end

@enduml

Code snippet

HAL1

HAL2

uint32_t Address = HDPLx_START_ADDRESS;
FLASH_EraseInitTypeDef EraseInitStruct;
const uint32_t QUADWORD[4] = {
  0x12345678, 0x87654321, 0x12344321, 0x56788765
};

HAL_FLASH_Unlock();

/* Increment OBK-HDPL value */
HAL_SBS_SetOBKHDPL(SBS_OBKHDPL_INCR_x);

/* Unlock OBK area */
HAL_FLASHEx_OBK_Unlock();

/* Erase the OBK alternate area */
EraseInitStruct.TypeErase = FLASH_TYPEERASE_OBK_ALT;
HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);

/* Swap OBK: HDPLx data copied from current to alternate OBK sector */
HAL_FLASHEx_OBK_Swap(FLASH_OBK_SWAP_OFFSET_HDPLx);

while (Address < HDPLx_END_ADDRESS)
{
  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD_OBK, Address, (uint32_t)QUADWORD) == HAL_OK)
  {
    Address = Address + offset;
  }
}

HAL_FLASH_Lock();
HAL_FLASHEx_OBK_Lock();
hal_flash_handle_t flash_handle;
uint32_t Address = HDPLx_START_ADDRESS;
const uint32_t QUADWORD[4] = {
  0x12345678, 0x87654321, 0x12344321, 0x56788765
};

HAL_FLASH_Init(&flash_handle, HAL_FLASH);
HAL_FLASH_ITF_Unlock(HAL_FLASH);

/* Increment OBK-HDPL value */
HAL_SBS_SetHDPLevelValue(HAL_SBS_OBKHDPL_INCR_x);

/* Unlock OBK area */
HAL_FLASH_ITF_OBK_Unlock(HAL_FLASH);

/* OBK mass erase */
HAL_FLASH_OBK_MassErase(HAL_FLASH, TIMEOUT);

/* OBK swap pages */
HAL_FLASH_OBK_SwapPages(HAL_FLASH, HAL_FLASH_ITF_OBK_AREA_x);

/* Set the programming mode according to the FLASH OBK granularity */
HAL_FLASH_SetProgrammingMode(&flash_handle, HAL_FLASH_PROGRAM_QUADWORD);

HAL_FLASH_OBK_ProgramByAddr(&flash_handle, HAL_FLASH_OBK_CURRENT_PAGE, Address, QUADWORD, 4, TIMEOUT);

HAL_FLASH_ITF_OBK_Lock(HAL_FLASH);
HAL_FLASH_ITF_Lock(HAL_FLASH);

Key changes

  1. HAL2 provides dedicated OBK APIs (unlock/swap/mass erase/program) split across ITF and IO.

  2. OBK programming in HAL2 is handle-based and uses buffer-oriented *_ProgramByAddr APIs.

  3. The SBS configuration step remains, but OBK control is better isolated from standard user FLASH services.